<?php
/*
 *@控制器基类
 */

class Control{
	var $smarty;
	var $encrypt_code;
	function __construct(){
		//初始化session
		/*$this->_session = new MemcacheSession( '192.168.16.87:11211','BHB_ID');
        define('SESS_ID', $this->_session->get_session_id());
        $this->_session->my_session_start();*/
        //和品牌尾货网使用同一个session
        $domain=explode(".",$_SERVER["HTTP_HOST"]);
		array_shift($domain);
		$domain=implode(".",$domain);
		ini_set('session.cookie_domain',".".$domain);
		if(isset($_COOKIE['session_id']))
		{
			session_id($_COOKIE['session_id']);
		}
        session_start();
        if(!isset($_SESSION["user_info"]) && $_REQUEST["c"]!="paycallback") {
        	header("location:http://www.ppsc.com.cn/index.php?act=login&synlogout=1");
        	exit();
        }
		//配置smarty
		$this->smarty=new Smarty();
		$this->smarty->template_dir = UPATH."/app/view/";
		$this->smarty->compile_dir = UPATH."/data/sm_compile/";
		$this->smarty->config_dir = UPATH."/data/config/sm_config/";
		$this->smarty->cache_dir = UPATH."/data/cache/sm_cache/";
		/*$this->smarty->templates(UPATH."/app/view"); //设置模板目录 
		$this->smarty->templates_c(UPATH."/data/sm_compile"); //设置编译目录 
		$this->smarty->cache(UPATH."/data/cache/sm_cache"); //缓存目录 
		*/
		$this->smarty->cache_lifetime = 86400; //缓存时间 
		$this->smarty->caching = false; //缓存方式 
		$this->smarty->left_delimiter = "{"; 
		$this->smarty->right_delimiter = "}"; 

		$this->smarty->register_modifier("substr","substr");

		//配置全局模板参数
		$this->site_url=site_url();
		$this->assign("site_url",site_url());
		$this->encrypt_code=md5(session_id());
		$this->assign("encrypt_code",$this->encrypt_code);

	}

	protected function out_result($data=array()) {
		if(!$data){
			$data=array();
			exit(json_encode(array("recode"=>1,"rs"=>$data)));
		}
		exit(json_encode(array("recode"=>1,"rs"=>$data)));
	}
	protected function out_error($data) {
		exit(json_encode(array("recode"=>0,"rs"=>$data)));
		
	}
	protected function assign($n,$v){
		$this->smarty->assign($n,$v);
	}

	protected function display($tpl){
		$this->smarty->display($tpl);
	}

	protected function _check_encrypt(){
		if(!isset($_REQUEST["encrypt_code"])) $this->out_error("no_encrypt");
		if(trim($_REQUEST["encrypt_code"])!=$this->encrypt_code) $this->out_error("wrong_encrypt");
	}

	protected function _cue($title="提交成功",$content="",$flg=1,$returl=""){
		$this->assign("cue_title",$title);
		$this->assign("cue_content",$content);
		if(!$returl) $returl=$_SERVER["HTTP_REFERER"];
		$this->assign("cue_returl",$returl);
		$this->assign("flg",$flg);
		$this->display("cue.html");
		exit();
	}

	protected function clear_cache(){
		$this->smarty->clear_all_cache();
	}

}


?>